﻿// -----------------------------------------------------------------------
// <copyright file="BinaryContent.cs" company="Zen Design Corp">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Zen.BlackBerry.Common
{
	using System;
	using System.IO;

	/// <summary>
	/// Represents binary content, automatically sets the appropriate content
	/// type and content transfer encoding header for a binary payload to
	/// 'binary'.
	/// </summary>
	/// <remarks>
	/// If requested, base64 encoding will be done on the binary content and
	/// the content transfer encoding header will be set to base64.
	/// </remarks>
	public class BinaryContent : SinglePartContent
	{
		private byte[] _content;
		private bool _useBase64Encoding;

		/// <summary>
		/// Initializes a new instance of the <see cref="BinaryContent"/> class.
		/// </summary>
		/// <param name="content">The content.</param>
		/// <param name="useBase64Encoding">if set to <c>true</c> [use base64 encoding].</param>
		public BinaryContent(byte[] content, bool useBase64Encoding)
		{
			_content = content;
			_useBase64Encoding = useBase64Encoding;
			SetContentType();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="BinaryContent"/> class.
		/// </summary>
		/// <param name="pathName">Name of the path.</param>
		/// <param name="useBase64Encoding">if set to <c>true</c> [use base64 encoding].</param>
		public BinaryContent(string pathName, bool useBase64Encoding)
		{
			_content = GetBytesFromFile(pathName);
			_useBase64Encoding = useBase64Encoding;
			SetContentType();
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="BinaryContent"/> class.
		/// </summary>
		/// <param name="uri">The URI.</param>
		/// <param name="useBase64Encoding">if set to <c>true</c> [use base64 encoding].</param>
		public BinaryContent(Uri uri, bool useBase64Encoding)
		{
			_content = GetBytesFromUrl(uri);
			_useBase64Encoding = useBase64Encoding;
			SetContentType();
		}

		/// <summary>
		/// Gets a value indicating whether this instance is empty.
		/// </summary>
		/// <value><c>true</c> if this instance is empty; otherwise, <c>false</c>.</value>
		public override bool IsEmpty
		{
			get
			{
				return _content == null;
			}
		}

		/// <summary>
		/// Checks the extension.
		/// </summary>
		/// <param name="fileExtension">The file extension.</param>
		protected override void CheckExtension(string fileExtension)
		{
		}

		/// <summary>
		/// Writes the content to the specified <see cref="StreamWriter"/>.
		/// </summary>
		/// <param name="writer">The writer.</param>
		protected override void WriteContentTo(StreamWriter writer)
		{
			if (_useBase64Encoding)
			{
				writer.WriteRawBase64(_content, 0, _content.Length);
			}
			else
			{
				writer.WriteRaw(_content, 0, _content.Length);
			}
		}

		private void SetContentType()
		{
			ContentType = "application/octet-stream";
			if (!_useBase64Encoding)
			{
				AddHeader("Content-Transfer-Encoding", "binary");
			}
			else
			{
				AddHeader("Content-Transfer-Encoding", "base64");
			}
		}
	}
}
